package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/16 11:10
 */
//后台系统-->查询用户
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        //返回分页结果对象
        return pageResult;
    }


    @RequestMapping("/findById")
    public Result findById(Integer id){
        //程序健壮性和安全性处理
        try {
            //调用roleService完成查询
            Role role= roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/findPermissionIdByRoleId")
    public Result findPermissionIdByRoleId(Integer id){
        //程序健壮性和安全性处理
        try {
            //调用roleService完成查询
            List<Integer> permissionIds = roleService.findPermissionIdByRoleId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }



    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds){
        //程序健壮性和安全性处理
        try {
            //调用roleService完成查询
            roleService.edit(role,permissionIds);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        //程序健壮性和安全性处理
        try {
            roleService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        //程序健壮性和安全性处理
        try {
            roleService.add(role,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            //添加信息失败,返回失败提示信息
            return  new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //添加信息成功,返回成功提示信息
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        //调用roleService完成查询
        List<Role> roleList = roleService.findAll();
        if(roleList != null && roleList.size() > 0){//表示有检查项
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,roleList);
        }
        return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
    }




}
